package com.tj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tj.mapper.EmployeeMapper;
import com.tj.pojo.Employee;
import com.tj.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    private Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //System.out.println(employee);

        //1.将页面提交的密码password进行md5加密处理
        String password=employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.查询数据库是否存在username
        Employee res=employeeService.selectByUsername(employee);

        //3.判断是否对于null
        if(res==null) return Result.error("登录失败");

        //3.判断密码是否正确
        if(!res.getPassword().equals(password)) return Result.error("登录失败");

        //4.查看该员工是否禁用(1-可用，0-禁用）
        if(res.getStatus()==0) return Result.error("该账号已禁用");

        //5.登录成功，将员工id存入session,并返回结果
        request.getSession().setAttribute("employee",res.getId());
        return Result.success(res);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    private Result<String> logout(HttpServletRequest request) {
        //1.清除session
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 添加员工
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    private Result<String> addEmployee(@RequestBody Employee employee,HttpServletRequest request) {

        // 1.判断账号是否存在
        Employee res = employeeService.selectByUsername(employee);
        if(res!=null)  return Result.error("创建失败");

        //2.设置新密码password并进行md5加密处理
        String password="123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);

        //3.设置创建时间，更新时间
        Date date=new Date();
        employee.setCreateTime(date);
        employee.setUpdateTime(date);

        //4.设置id,创始人(==id),更新人(==id),status
        employee.setId((Long)request.getSession().getAttribute("employee"));
        employee.setId(employee.getId());
        employee.setCreateUser(employee.getId());
        employee.setUpdateUser(employee.getId());
        employee.setStatus(1);

        //5.添加信息到数据库
        employeeService.addEmployee(employee);
        return Result.success("创建成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    private Result<Page<Employee>> getPage(int page,int pageSize,String name) {

        //1.获取分页数据
        List<Employee> res=employeeService.getPageEmployee(page,pageSize,name);

        //2.生成Page类并赋值
        Page<Employee> r=new Page<>();
        r.setRecords(res);
        r.setTotal(res.size());

        //3.根据结果是否为空,返回相应数据
        if(res.size()!=0)  return Result.success(r);
        else return Result.error("没有数据");
    }

    /**
     * 更改员工账号状态(status)
     * @param employee
     * @return
     */
    @PutMapping
    private Result<String> changeStatus(@RequestBody Employee employee) {
        //System.out.println(employee);

        //1.设置update_time,update_user
        employee.setUpdateTime(new Date());
        employee.setUpdateUser(employee.getId());

        //2.获取受影响的列数
        int l=employeeService.updateEmploy(employee);

        //2.根据结果l返回不同结果
        if(l>0) return Result.success("更新成功");
        else return Result.error("未查询到要更新的数据");
    }

    @GetMapping("/{id}")
    private Result<Employee> getEmployeeById(@PathVariable int id) {

        //1.获取员工信息
        Employee res=employeeService.getById(id);

        //返回结果
        return Result.success(res);

    }
}
